package com.baoli.blzj.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.baoli.blzj.bean.home.HomePagerAdvertBean;
import com.baoli.blzj.ui.common.LunBoWebActivity;
import com.bumptech.glide.Glide;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.baoli.blzj.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页轮播图适配器 by John
 */
public class HomeRollAdapter extends StaticPagerAdapter {

    private List<HomePagerAdvertBean> totalList;
    private Context context;
    private LayoutInflater inflater;
    private ImageView pager_photos;
    private int from;

    public HomeRollAdapter(Context context) {
        this.context = context;
        this.totalList = new ArrayList<>();
        this.inflater = (LayoutInflater) LayoutInflater.from(context).getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setList(List<HomePagerAdvertBean> hBean) {
        totalList.clear();
        append(hBean);
    }

    public void setfrom(int from) {
        this.from = from;
    }

    public void append(List<HomePagerAdvertBean> hBean) {
        totalList.addAll(hBean);
        notifyDataSetChanged();
    }

    @Override
    public View getView(ViewGroup container, final int position) {
        //加载网络图片
        container = (ViewGroup) inflater.inflate(R.layout.view_pager_photo, null);
        pager_photos = (ImageView) container.findViewById(R.id.pager_photos);

        if ("".equals(totalList.get(position).getImg_url())) {
            pager_photos.setImageResource(R.drawable.ic_launcher);
        } else {
            Glide.with(context)
                    .load(totalList.get(position).getImg_url())
                    .crossFade()
                    .animate(R.anim.anim_photo)
                    .placeholder(R.drawable.ic_launcher)
                    .error(R.drawable.ic_launcher)
                    .into(pager_photos);
            container.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LunBoWebActivity.class);
                    if (totalList.get(position).getImg_on() != null) {
                        if (totalList.get(position).getImg_on().equals("")) {

                        } else {
                            intent.putExtra("url", totalList.get(position).getImg_on());
                            switch (from) {
                                case 1:
                                    intent.putExtra("name", "君办头条");
                                    break;
                                case 2:
                                    intent.putExtra("name", "商贸头条");
                                    break;
                                case 3:
                                    intent.putExtra("name", "商务头条");
                                    break;
                                default:
                                    break;
                            }

                            context.startActivity(intent);
                        }
                    }
                }
            });
        }
        return container;
    }

    @Override
    public int getCount() {
        return totalList.size();
    }
}
